var BaseHost = "http://202.99.114.74:57115";
//按键事件注册
var _keyName = "";
var _epg = false;
var hasOpen = false;
var area = "";
var keyBase = {
	record: "",
	STB_Key: function (keyEvent) {
		keyEvent = keyEvent ? keyEvent : window.event;
		var prevent = false;
		var keyCode = keyEvent.keyCode || keyEvent.which || keyEvent.charCode;
		switch (keyCode) {
			// 方向键
			//"LEFT"左
			case 3: //大亚科技
			case 29:
			case 37: //ipanel 华为
			case 65361:
				_keyName = "KEY_LEFT";
				prevent = true;
				break;
				//上
			case 1: //大亚科技
			case 28: //
			case 38: //ipanel 华为
			case 65362:
				_keyName = "KEY_UP";
				prevent = true;
				break;
				//右
			case 4: //大亚科技
			case 30:
			case 39: //ipanel 华为
			case 65363:
				_keyName = "KEY_RIGHT";
				prevent = true;
				break;
				//下
			case 2: //大亚科技
			case 31:
			case 40: //ipanel 华为
			case 65364:
				_keyName = "KEY_DOWN";
				prevent = true;
				break;
				// 翻页键
			case 33:
				_keyName = "KEY_PAGEUP";
				break;
			case 34:
				_keyName = "KEY_PAGEDOWN";
				break;
				// 返回/退格、回车/确认、空格键
			case 8:
			case 32: //创维安卓
			case 122: //ipanel2.0
			case 270:
			case 340: //新疆广电盒子
			case 640: //coship
			case 65367: //
				_keyName = "KEY_BACK"; //KEY_BACKSPACE
				//prevent = true;
				break;
			case 13: //ipanel 大亚科技 华为
			case 65293:
				_keyName = "KEY_ENTER"; //KEY_OK
				prevent = true;
				break;
			case 15:
				_keyName = "KEY_SPACEBAR";
				break;
				// 数字键
			case 48: //ipanel 大亚科技 华为
			case 96:
				_keyName = "KEY_0";
				break;
			case 49:
			case 97:
				_keyName = "KEY_1";
				break;
			case 50:
			case 98:
				_keyName = "KEY_2";
				break;
			case 51:
			case 99:
				_keyName = "KEY_3";
				break;
			case 52:
			case 100:
				_keyName = "KEY_4";
				break;
			case 53:
			case 101:
				_keyName = "KEY_5";
				break;
			case 54:
			case 102:
				_keyName = "KEY_6";
				break;
			case 55:
			case 103:
				_keyName = "KEY_7";
				break;
			case 56:
			case 104:
				_keyName = "KEY_8";
				break;
			case 57:
			case 105:
				_keyName = "KEY_9";
				break;
				// 功能键
				//音量+
			case 259:
				_keyName = "KEY_VOL_UP";
				break;
				//音量-
			case 260:
				_keyName = "KEY_VOL_DOWN";
				break;
			case 261:
				_keyName = "KEY_MUTE";
				break;
			case 262:
				_keyName = "KEY_TRACK";
				break;
			case 263:
				_keyName = "KEY_PAUSE_PLAY";
				break;
				// 播放键
			case 264:
				_keyName = "KEY_FAST_FORWARD"; //快进键
				break;
			case 265:
				_keyName = "KEY_FAST_REWIND"; //快退键
				break;
			case 266:
				_keyName = "KEY_GO_END";
				break;
			case 267:
				_keyName = "KEY_GO_BEGINNING";
				break;
			case 268:
				_keyName = "KEY_INFO";
				break;
			case 269:
				_keyName = "KEY_INTERX";
				break;
			case 270:
				_keyName = "KEY_STOP";
				break;
			case 271:
				_keyName = "KEY_POS";
				break;
			case 1105:
				_keyName = "KEY_SEARCH";
				break;
			case 1240:
				_keyName = "KEY_CAPTION";
				break;
			case 1246:
				_keyName = "KEY_INTERACTIVE";
				break;
				//虚拟事件
			case 768:
				if (typeof (Utility) != "object" || !("getEvent" in Utility)) {
					return;
				}
				var event = Utility.getEvent();
				var typeStr = event.type;
				if (typeof (typeStr) == "undefined" || typeStr == "") {
					var tArray = new Array();
					tArray = event.split(",");
					var temp = tArray[0];
					var index = temp.indexOf(":") + 2;
					typeStr = temp.substring(index, temp.length - 1);
				}
				switch (typeStr) {
					case "EVENT_MEDIA_ERROR": //视频播放出错
						_keyName = "EVENT_MEDIA_ERROR";
						break;
					case "EVENT_MEDIA_END": //视频播放结束
						_keyName = "EVENT_MEDIA_END";
						break;
					default:
						break;
				}
				break;
			default:
				_keyName = "";
				break;
		}

		if (prevent) {
			//禁止左右上下键的时候屏幕滚动及事件冒泡
			if (keyEvent.preventDefault) {
				keyEvent.preventDefault();
			} else if (keyEvent.returnValue) {
				keyEvent.returnValue = false;
			} else if (keyEvent.stopPropagation) {
				evt.stopPropagation();
			}
		}
		//调用自定义按键函数
		keyBase.keyEvents(keyCode);
		return _keyName;
	},
	//按键事件注册 监听_keyName值
	keyEvents: function (keyCode) {
		try {
			//阻止非数字按键唤醒日志
			//判断是否是数字 
			if (!isNaN(_keyName.substr(_keyName.length - 1, 1))) {
				this.record += _keyName + "|";
				if (_keyName == "KEY_9") { //数字键9，清空数字键存储，或清空一条日志
					//evm.clearDebug();
					this.record = "";
				}
				if (this.record == "KEY_1|KEY_1|") { //个人中心-按键为1+3组合时，进入测试页
					console.log(111111);
					if (!hasOpen) {
						document.getElementById("mοuse").style.display = "block";
						hasOpen = true;
						area = "move";
						keyBotton._onBlur();
						this.record = "";
					} else {
						document.getElementById("mοuse").style.display = "none";
						hasOpen = false;
						keyBotton._init();
						this.record = "";
					}
				} else if (this.record == "KEY_1|KEY_0|") { //按键为1+0组合时，打开或关闭屏幕日志
					//evm.openDebug();
				}
			}
			// var p = document.createElement("p");
			// p.innerHTML = keyCode;
			// //evm.setDebug("键值：" + keyCode);
			// document.body.appendChild(p);
			//函数位于其他页面
			keyevent();
			console.log(this.record);
		} catch (error) {
			console.log(error);
		}

	}
};

if (typeof (window.mp) != 'undefined') {
	_epg = true;
}
if (_epg || /webkit/g.test(navigator.userAgent.toLowerCase())) {
	document.onkeydown = keyBase.STB_Key;
	console.log("document.onkeydown ");
} else {
	document.onkeypress = keyBase.STB_Key;
	console.log("document.onkeypress");
}
var divEle = document.createElement("div");
divEle.id = "mοuse";
divEle.style =
	"display:none;position: absolute; left: 0px; top: 0px; background-color: #f60606;width: 10px;height: 10px;z-index: 99999999;";
document.body.appendChild(divEle);
var moveX = 0;
var moveY = 0;
// var evclickObj = new Event("click");
var evDownObj = new Event('mousedown');
// var evMoveObj = new Event('mousemove');
// var evUpObj = new Event('mouseup');
// var evoverObj = new Event("mouseover");
document.onclick = function (e) {
	console.log(e);

}
var oldIEle = null;
var clientWidths = document.documentElement.clientWidth - 10;
var mοuse = {
	Ele: document.getElementById("mοuse"),
	_setXY: function () {
		this.Ele.style.top = moveY + "px";
		this.Ele.style.left = moveX + "px";
		var nowEle = this._getElementFromPoint();
		var pEle = this._getElementFromPoint(2);
		console.log(nowEle, pEle);

		console.log(nowEle.nodeName, pEle.nodeName);
		if ((nowEle.nodeName == "I" && pEle.nodeName == "A") || (nowEle.nodeName == "DIV" && evm.hasClass(nowEle, "x-item")) || nowEle.nodeName == "BUTTON") {
			var focusEleArr = document.getElementsByClassName("afoucs");
			console.log(focusEleArr);

			for (i in focusEleArr) {
				console.log(focusEleArr[i]);
				if (focusEleArr.length > 0)
					evm.removeClass(focusEleArr[i], "afoucs");
			}
			evm.addClass(nowEle, "afoucs");
			oldIEle = nowEle;
		} else {
			if (oldIEle)
				evm.removeClass(oldIEle, "afoucs");
		}

		nowEle.dispatchEvent(evDownObj);
		//this._getElementFromPoint().focus();
	},
	_getBoundingClientRect: function () {
		return this.Ele.getBoundingClientRect();
	},
	_getElementFromPoint: function (index) {
		var EleArr = document.elementsFromPoint(this._getBoundingClientRect().x, this
			._getBoundingClientRect().y);
		console.log(EleArr);
		return EleArr[(index || 1)];
	},
	_left: function () {
		var x = this._getBoundingClientRect().x;
		if (moveX > 0) {
			moveX -= 10;
		}
		// console.log(this._getElementFromPoint());
		this._setXY();
	},
	_right: function () {
		var x = this._getBoundingClientRect().x;
		if (moveX < clientWidths) {
			moveX += 10;
		}
		// console.log(this._getElementFromPoint());
		this._setXY();
	},
	_down: function () {
		var y = this._getBoundingClientRect().y;
		// console.log(y);

		if (moveY < 720) {
			moveY += 10;
		}
		// console.log(this._getElementFromPoint());
		this._setXY();
	},
	_up: function () {
		var y = this._getBoundingClientRect().y;
		//console.log(y);
		if (moveY > 0) {
			moveY -= 10;
		}
		//console.log(this._getElementFromPoint());
		this._setXY();
	},
	_enter: function () {
		// this._getElementFromPoint().dispatchEvent(evclickObj);
		// this._getElementFromPoint().click();
		var evObj = document.createEvent('MouseEvents');
		console.log(this._getBoundingClientRect());
		var nowEle = this._getElementFromPoint();
		var X = moveX + window.screenLeft;
		var Y = moveY + window.screenTop + window.outerHeight - window.innerHeight;
		console.log(X, Y);
		if (nowEle.className == "tic-btn sendmsg") {
			return;
		} else
		if (nowEle.nodeName == "INPUT") {
			nowEle.focus();
		} else {
			//nowEle.click();
			evObj.initMouseEvent('click', true, true, window, 1, X, X, moveX, moveY, false, false, false, false,
				0, null);
			this._getElementFromPoint().dispatchEvent(evObj);
		}

	}
};

function getEle(strName) {
	var EleArr = [];
	strName.forEach(className => {
		console.log(className, document.getElementsByClassName(className));

		EleArr.push(document.getElementsByClassName(className)[0]);
	});
	return EleArr;
}
var iEleArr = document.getElementsByTagName("i");
console.log(iEleArr);
var keyBotton = null;
var choiceFqa = null;
hasCreateEle("ivu-btn ivu-btn-primary ivu-btn-large", function (ele) {
	ele.click();
});
hasCreateEle("hand-up", function () {
	keyBotton = keyBottons();
	keyBotton._init();
	//buildTeacherELe();
});
hasCreateEle("bg-teacher", function () {
	buildTeacherELe();
});

function hasexit() {
	var timer = null;
	timer = setInterval(function () {
		var eleArr = document.getElementsByClassName("ivu-modal-confirm-body")
		if (eleArr.length > 0) {
			if (eleArr[0].textContent == "确定退出吗？") {
				clearInterval(timer);
				exit._init();
			}
		}

	}, 1000);
}
hasexit();
sti();

function sti() {
	hasCreateEle("tic-dialog__ft", function (ele) {
		qiandao._init(ele);
	});
}
var exit = {
	_index: 0,
	_init() {
		area = "exit";
		evm.addClass(document.getElementsByClassName("ivu-btn ivu-btn-text ivu-btn-large")[0], "afoucs");
		this._index = 0;
	},
	_left() {
		this._index = 0
		evm.removeClass(document.getElementsByClassName("ivu-btn ivu-btn-primary ivu-btn-large")[0], "afoucs");
		evm.addClass(document.getElementsByClassName("ivu-btn ivu-btn-text ivu-btn-large")[0], "afoucs");
	},
	_right() {
		this._index = 1
		evm.removeClass(document.getElementsByClassName("ivu-btn ivu-btn-text ivu-btn-large")[0], "afoucs");
		evm.addClass(document.getElementsByClassName("ivu-btn ivu-btn-primary ivu-btn-large")[0], "afoucs");
	},
	_enter() {
		if (this._index == 0) {
			document.getElementsByClassName("ivu-btn ivu-btn-text ivu-btn-large")[0].click();
			hasexit();
			keyBotton._init();
		} else {
			document.getElementsByClassName("ivu-btn ivu-btn-primary ivu-btn-large")[0].click();
		}
	},
}

function keyBottons() {
	return {
		_focusEle: {
			"0": getEle(["hand-up", "tic-icon-setting", "tic-icon-info", "tic-icon-exit"]),
			"1": getEle(["bg-teacher"]),
			"2": getEle(["tic-input", "tic-btn sendmsg"]),
		},
		_focus: 0,
		_blur: 0,
		_blurRow: 0,
		_row: 0,
		_showVideo: false,
		_init() {
			area = "keyBotton";
			this._onFocus();
		},
		_onFocus() {
			this._onBlur();
			this._focusEle["1"] = getEle(["bg-teacher"]);
			var focusEle = this._focusEle[this._row][this._focus];
			console.log(focusEle);
			if (focusEle.nodeName == "INPUT") {
				focusEle.focus();
			} else {
				this._focusEle[2][0].blur();
			}
			evm.addClass(focusEle, "afoucs");


		},
		_onBlur() {
			console.log(this._focusEle[this._blurRow][this._blur]);
			evm.removeClass(this._focusEle[this._blurRow][this._blur], "afoucs");
			this._blur = this._focus;
			this._blurRow = this._row;
		},
		_left() {
			if (this._focus > 0) {
				this._focus--;
			}
			this._onFocus();
			// var iEleArr = document.getElementsByTagName("i");
			// var nowEle = getLeft(document.getElementsByTagName("i")[this._focus]);
			//console.log(document.getElementsByTagName("i")[this._focus], nowEle)
			// var that = this;
			// mins(iEleArr, this._focus, nowEle);
			//Math.max.apply(null, arr);
		},
		_right() {
			if (this._focus < this._focusEle[this._row].length - 1) {
				this._focus++;
			}
			this._onFocus();
		},
		_up() {
			if (this._row > 0) {
				this._row--;
				this._focus = 0;
			}
			this._onFocus();
		},
		_down() {
			if (this._row < 2) {
				this._row++;
				this._focus = 0;
			}
			this._onFocus();
		},
		_enter() {
			var focusEle = this._focusEle[this._row][this._focus];
			if (this._row == 1) {
				if (!this._showVideo) {
					this._showVideo = true;
					buildTeacherELe();
					document.getElementById("full0").click();
				} else {
					this._showVideo = false;
					document.getElementById("video").style.display = "none";
				}

			} else
			if (focusEle.nodeName == "BOTTON") {
				focusEle.click();
			} else {
				focusEle.parentNode.click();
				if (focusEle.className.indexOf("hand-up") > -1) {
					hasCreateEle("hand-down", function () {
						evm.addClass(focusEle.parentNode.children[0], "afoucs");
					});
				} else if (focusEle.className.indexOf("hand-down") > -1) {
					hasCreateEle("hand-up", function () {
						evm.addClass(focusEle.parentNode.children[0], "afoucs");
					});
				} else if (focusEle.className.indexOf("tic-icon-setting") > -1) {
					hasCreateEle("form-list__btn", function () {
						setPaenl._init();
					});
				}
			}
		},

	}
}
//计时器判断元素是否存在
function hasCreateEle(className, callBack) {
	var getEleTimer = null;
	clearInterval(getEleTimer);
	getEleTimer = setInterval(() => {
		if (document.getElementsByClassName(className).length > 0) {
			clearInterval(getEleTimer);
			callBack(document.getElementsByClassName(className)[0]);
		}
	}, 500);
}
var setPaenl = {
	_Ele: null,
	_init() {
		this._Ele = document.getElementsByClassName("form-list__btn")[0].children[0];
		var selectELeArr = document.getElementsByClassName("tic-select");
		selectELeArr.forEach(element => {
			element.multiple = "multiple";
			element.size = "4";
		});
		area = "setPaenl";
		evm.addClass(this._Ele, "afoucs");
	},
	_enter() {
		console.log(this._Ele);

		this._Ele.click();
		evm.removeClass(this._Ele, "afoucs");
		keyBotton._init();
	}
}
var qiandao = {
	_data() {
		return {
			_focus: 0,
			_row: 0,
			_rowS: 0,
			_oldFocus: 0,
			_oldRow: 0,
			_EleArr: [],
			_enterELe: null,
			_hasButton: false,
			_hasBack: false,
			_hasclickEnter: false,
			_msg: false,
		}
	},
	_init(ele) {
		_extend(this, this._data.apply(this));
		var aTag = document.getElementsByClassName("tic-tag");
		console.log(aTag.length / 4);

		this._rowS = Math.ceil(aTag.length / 4) - 1;
		console.log(this._rowS);
		this._enterELe = ele.children[0];
		area = "qiandao";
		this._EleArr = aTag;
		if (aTag.length > 0) {
			_extend(this, this._direction.apply(this));
			this._onFocus();
		} else {
			this._msg = true;
			this._hasButton = true;
			evm.addClass(this._enterELe, "afoucs");
		}
	},
	_onFocus() {
		this._onBlur();
		evm.addClass(this._EleArr[this._focus + this._row * 4], "afoucs");
	},
	_onBlur() {
		console.log(this._EleArr, this._oldFocus, this._oldRow);

		evm.removeClass(this._EleArr[this._oldFocus + this._oldRow * 4], "afoucs");
		this._oldFocus = this._focus;
		this._oldRow = this._row;
	},
	_direction() {
		return {
			_left() {
				console.log(11111111111111111);

				if (this._focus > 0) {
					this._focus--;
					this._onFocus();
				}

			},
			_right() {
				console.log(this._row, this._rowS, this._focus);

				if (this._row < this._rowS || this._rowS == 0) {
					if (this._focus < 3) {
						this._focus++;
					}
				} else {
					if (this._focus < this._EleArr.length % 4) {
						this._focus++;
					}
				}
				this._onFocus();
			},
			_up() {
				if (this._hasButton) {
					this._hasButton = false;
					evm.removeClass(this._enterELe, "afoucs");
					this._onFocus();
				} else {

					if (this._row > 0) {
						this._row--;
						this._onFocus();
					} else {
						if (this._hasclickEnter) {
							this._onBlur();
							this._hasBack = true;
							evm.addClass(document.getElementsByClassName("tic-btn-close")[0], "afoucs");
						}
					}
				}

			},
			_down() {
				if (this._row < this._rowS) {
					this._row++;
					this._onFocus();
				} else {
					this._hasButton = true;
					evm.addClass(this._enterELe, "afoucs");
					this._onBlur();
				}
			},
		}
	},

	_enter() {
		if (this._hasclickEnter) {
			document.getElementsByClassName("tic-btn-close")[0].click();
			keyBotton._init();
			sti();
		} else if (this._hasButton) {
			this._hasclickEnter = true;
			this._enterELe.click();
			if (this._msg) {
				keyBotton._init();
				sti();
			}
		} else {
			var nowEle = this._EleArr[this._focus + this._row * 4];
			nowEle.click();
			var timer = setInterval(function () {
				if (evm.hasClass(nowEle, "actived")) {
					clearInterval(timer);
					evm.addClass(nowEle, "afoucs");
				}
			});
		}
	}
}

function buildTeacherELe() {
	var videoELeArr = document.getElementsByClassName("tic-number__list");
	videoELeArr.forEach((element, index) => {
		console.log(element, index);
		var nowEle = document.getElementById("full" + index);
		if (!nowEle) {
			var imgEle = document.createElement("div");
			imgEle.id = "full" + index;
			imgEle.onclick = createvideo;
			imgEle.style = "display:none;width: 30px;height: 30px;position: absolute;top: 0px;right: 0px;background-color: #fff;z-index: 9;";
			element.addEventListener("mouseout", function (e) {
				document.getElementById("full" + index).style.display = "none";
				console.log(22222222, e);
			});
			element.onmouseover = function (e) {
				document.getElementById("full" + index).style.display = "block";
				console.log(11111111, e);
			};
			element.appendChild(imgEle);
		}
	});
}

function createvideo(playEle) {
	var videoParentEle = document.getElementById("video");
	var videoEle = null;
	if (videoParentEle) {
		videoParentEle.style.display = "block";
		videoEle = document.getElementById("video").firstChild;
	} else {
		var divEleD = document.createElement("div");
		var divEleS = document.createElement("div");
		var divEle = document.createElement("video");
		divEleD.id = "video";
		divEle.autoplay = "autoplay";
		divEleS.onclick = function () {
			document.getElementById("video").style.display = "none";
		};
		divEleS.style = "width: 40px;height: 40px;position: absolute;bottom: 30px;right: 10px;background-color: #fff;z-index: 9;";
		divEle.style =
			"position: absolute; left: 0px; top: 0px; background-color: black;width: 1280px;height: 720px;transform: rotateY(180deg);";
		divEleD.style =
			"position: absolute; left: 0px; top: 0px; background-color: black;width: 1280px;height: 720px;z-index: 99999;";
		videoEle = divEle;
		divEleD.appendChild(divEle);
		divEleD.appendChild(divEleS);
		document.body.appendChild(divEleD);
	}
	console.log(playEle.target.parentNode.firstChild.firstChild.getElementsByTagName("video")[0].srcObject);
	console.log(videoEle)
	videoEle.srcObject = playEle.target.parentNode.firstChild.firstChild.getElementsByTagName("video")[0].srcObject;
	videoEle.play();
}
/*获取元素的纵坐标*/
function getTop(e) {
	var offset = e.offsetTop;
	if (e.offsetParent != null) {
		offset += getTop(e.offsetParent);
	}
	return offset;
}
/*获取元素的横坐标*/
function getLeft(e) {
	var offset = e.offsetLeft;
	if (e.offsetParent != null) {
		offset += getLeft(e.offsetParent);
	}
	return offset;
}

function keyevent() {
	var keyobj;
	if (area == "move") {
		keyobj = mοuse;
	} else if (area == "keyBotton") {
		keyobj = keyBotton;
	} else if (area == "setPaenl") {
		keyobj = setPaenl;
	} else if (area == "qiandao") {
		keyobj = qiandao;
	} else if (area == "exit") {
		keyobj = exit;
	} else {
		return null;
	}


	switch (_keyName) {
		case "KEY_1":
			if (typeof keyobj._left == "function") {
				keyobj._left();
			}
			break;
		case "KEY_LEFT":
			if (typeof keyobj._left == "function") {
				keyobj._left();
			}
			break;
		case "KEY_RIGHT":
			if (typeof keyobj._right == "function") {
				keyobj._right();
			}
			break;
		case "KEY_DOWN":
			if (typeof keyobj._down == "function") {
				keyobj._down();
			}
			break;
		case "KEY_UP":
			if (typeof keyobj._up == "function") {
				keyobj._up();
			}
			break;
		case "KEY_ENTER":
			if (typeof keyobj._enter == "function") {
				keyobj._enter();
			}
			break;
		case "KEY_BACK":
			if (typeof keyobj._back == "function") {
				keyobj._back();
			} else {
				BackParent();
			}
			break;
		default:
			break;
	}
}